﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace DungeonGenerator
{
    class Dijkstra
    {
        GraphData graphData = GraphData.CreateGraphSingleton();
        PrioQueue pqueue;

        public Dijkstra(Node source)
        {
            source.Distance = 0;
            pqueue = new PrioQueue();
            foreach (Node i in graphData.Nodes)
            {
                if (i != source)
                {
                    i.Distance = 999999;
                    i.Previous = null;
                }
                pqueue.Enqueue(i, i.Id);
            }
            //Debug.Log("size que: " + pqueue.total_size);
            while (pqueue.total_size != 0)
            {
                Node u;
                u = pqueue.Peek() as Node;
                pqueue.Dequeue(u.Id);
                //Debug.Log("Node " + u.Id + " dist: " + u.Distance);

                foreach (Node v in u.Connection)
                {
                    int temp;
                    temp = u.Distance + v.Weight;
                    if (temp < v.Distance)
                    {
                        v.Distance = temp;
                        v.Previous = u;
                        if (pqueue.total_size > 0)
                        {
                            pqueue.Dequeue(v.Id);
                            pqueue.Enqueue(v, v.Id);
                        }
                        //Debug.Log("jarak tempuh " + temp);
                    }

                }
            }
        }
    }
}